using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;
using System.Web.UI;
using Castle.ActiveRecord;
using NHibernate.Expression;

namespace WOODIFramework.Model
{
    [ActiveRecord("BasicGrid")]
    public class BasicGrid : PageItem
    {
        #region Fields

        private Guid _basicGridId;
        private IList _columns = new ArrayList();
        private string _collectionName;
        private string _boundObjectName;
        private bool _showHeader;

        #endregion

        #region Properties

        [JoinedKey("BasicGridID")]
        public Guid BasicGridId
        {
            get { return _basicGridId; }
            set { _basicGridId = value; }
        }

        [HasAndBelongsToMany(typeof(BasicGridColumn), Table = "BasicGridColumn_LK", ColumnKey = "BasicGridId", ColumnRef = "BasicGridColumnId")]
        public IList Columns
        {
            get { return _columns; }
            set { _columns = value; }
        }

        [Property]
        public string BoundObjectName
        {
            get { return _boundObjectName; }
            set { _boundObjectName = value; }
        }

        [Property]
        public string CollectionName
        {
            get { return _collectionName; }
            set { _collectionName = value; }
        }

        [Property]
        public bool ShowHeader
        {
            get { return _showHeader; }
            set { _showHeader = value; }
        }

        #endregion

        #region Constructors

        private BasicGrid()
        {
            this.Id = Guid.NewGuid();
            this.BasicGridId = this.Id;
        }


        private BasicGrid(string Name)
        {
            this.Id = Guid.NewGuid();
            this.BasicGridId = this.Id;
            this.Name = Name;
        }

        #endregion

        #region Public Methods

        #region Public Static Methods

        public static BasicGrid CreateBasicGrid()
        {
            return new BasicGrid();
        }

        public static BasicGrid CreateBasicGrid(string Name)
        {
            return new BasicGrid(Name);
        }

        #endregion

        public BasicGridColumn FindColumnByName(string ColumnName)
        {
            foreach (BasicGridColumn column in this.Columns)
            {
                if (column.Name == ColumnName)
                { return column; }
            }
            return null;
        }

        public override string RenderHTML()
        {
            StringBuilder stringBuilder = new StringBuilder();
            StringWriter stringWriter = new StringWriter(stringBuilder);
            HtmlTextWriter writer = new HtmlTextWriter(stringWriter);
            writer.WriteFullBeginTag("span");
            writer.WriteFullBeginTag("b");
            writer.Write(DisplayName);
            writer.WriteEndTag("b");
            writer.WriteEndTag("span");

            writer.WriteBeginTag("table");
            writer.WriteAttribute("border", "1");
            writer.WriteAttribute("class", "clearfix");
            writer.Write(HtmlTextWriter.TagRightChar);
            Object requiredObject = BoundCollections[BoundObjectName];
            IList collection = (IList)requiredObject;
            
            if (Columns.Count == 0)
            {
                if (ShowHeader)
                {
                    writer.WriteFullBeginTag("tr");
                    foreach (PropertyInfo itemProperty in collection.GetType().GetProperties())
                    {
                        writer.WriteFullBeginTag("th");
                        writer.Write(itemProperty.Name);
                        writer.WriteEndTag("th");
                    }
                    writer.WriteEndTag("tr");
                }

                foreach (Object collectionItem in collection)
                {
                    writer.WriteFullBeginTag("tr");

                    foreach (PropertyInfo itemProperty in collectionItem.GetType().GetProperties())
                    {
                        writer.WriteFullBeginTag("td");
                        writer.Write(itemProperty.GetValue(collectionItem, null).ToString());
                        writer.WriteEndTag("td");
                    }
                    writer.WriteEndTag("tr");
                }
            }
            else
            {
                if (ShowHeader)
                {
                    writer.WriteFullBeginTag("tr");
                    foreach (BasicGridColumn column in Columns)
                    {
                        writer.WriteBeginTag("th");
                        writer.WriteAttribute("width", column.Width.ToString());
                        writer.Write(HtmlTextWriter.TagRightChar);
                        if (column.IsBold)
                        { writer.WriteFullBeginTag("b"); }
                        writer.Write(column.DisplayName);
                        if (column.IsBold)
                        { writer.WriteEndTag("b"); }
                        writer.WriteEndTag("th");
                    }
                    writer.WriteEndTag("tr");
                }

                foreach (Object collectionItem in collection)
                {
                    writer.WriteFullBeginTag("tr");

                    foreach (BasicGridColumn column in Columns)
                    {
                        PropertyInfo itemProperty = collectionItem.GetType().GetProperty(column.Name);

                        writer.WriteFullBeginTag("td");
                        if (column.IsBold)
                        { writer.WriteFullBeginTag("b"); }
                        if (column.IsHyperlink)
                        {
                            writer.WriteBeginTag("a");
                            PropertyInfo boundProperty = collectionItem.GetType().GetProperty(column.DataBoundObjectProperties[BoundObjectName]);
                            writer.WriteAttribute("href", string.Format(column.HyperlinkFormatText, boundProperty.GetValue(collectionItem, null).ToString()));
                            writer.Write(HtmlTextWriter.TagRightChar);
                        }
                        writer.Write(itemProperty.GetValue(collectionItem, null).ToString());
                        if (column.IsHyperlink)
                        { writer.WriteEndTag("a"); }
                        if (column.IsBold)
                        { writer.WriteEndTag("b"); }
                        writer.WriteEndTag("td");
                    }
                    writer.WriteEndTag("tr");
                }
            }

            writer.WriteEndTag("table");
            return stringWriter.ToString();
        }

        #endregion


    }
}
